<?php
namespace Shopmobile\Controller;
use Think\Controller;
class IndexController extends ExtendController {
	public $index_json = Array();
	public function ajax_user(){
		$this->ajaxReturn($this->user);
	}
	public function ajax_index(){
		$this->_init_index();
		$this->ajaxReturn($this->index_json);
	}
	public function index(){
		$this->_init_index();
		if(!$this->index_json["event"]){
			$this->assign("msg","当前时间没有活动！");
			$this->display("Index:not_allow");
			die();	
		}
		$this->assign("assets","shopmobile");
		$this->assign("seller_nick",$this->index_json["seller_nick"]);
		$this->assign("getway",$this->index_json["getway"]);
		$this->assign("user",$this->index_json["user"]);
		$this->assign("event",$this->index_json["event"]);
		$this->assign("cfg",$this->index_json["event"]);
		$this->assign("gifts",$this->index_json["gifts"]);
		$this->assign("mygifts",$this->index_json["mygifts"]);
		$this->display($this->index_json["display"]);
	}
	private function _init_index(){
		$EV = M("Events"); //活动列表，同时只有1个活动
		$wh["sid"]=$this->sid;
		$wh["start_time"]=array('lt',date("Y-m-d H:i:s"));
		$wh["end_time"]=array('egt',date("Y-m-d H:i:s"));
		$fields = "`id`,`front_mob_cfg`,`curtool`,notice,brandid,collect,tms_newcomer,tms_everyday,tms_cartadd,tms_cartadd_limit,orderset,andor,order_pay_time_start,order_pay_time_end,order_payment,cart_items,order_pay_times,order_success_times,tms_goodrate,lotts,wins";
		$event = $EV->where($wh)->getField($fields);
		// if(!$event){
			// $event = M("ShopCfg")->where(Array("sid"=>$this->sid))->getField($fields);
			// $daily=true;
		// }
		if(!$event){
			return;
		}

		$event = current($event);
		if($daily){
			$event["daily"] = $daily;
			$event["id"]=0; //日常活动的eid为0
		}
		if(C("TAE")){
			$event["cart_items_count"] = count(json_decode(urldecode_utf8($event["cart_items"]),true));
		}else{
			$event["cart_items_count"] = count(json_decode(urldecode($event["cart_items"]),true));
		}
		$event["front_mob_cfg"] = json_decode($event["front_mob_cfg"],true);

		$GFT = M("EventsGifts"); //当前活动的奖品列表
		$gifts = $GFT->where(Array("event_id"=>$event["id"]))
		             ->group("gift_id")->order("`level` ASC")
					 ->getField("`id`,`level`,`price`, `level_name`, `status`, `name`, `isshow`, `quantity`, `pic_url`");
		
		$UGFT = D("UsersGifts"); //提取中奖记录
		$UGFT->sid = $this->sid;
		$UGFT->eid = $event["id"];
		$UGFT->shop_uid = $this->shop_uid;
		$UGFT->getMyGifts();
		$mygifts = $UGFT->mygifts;
		
		
		$EVU = M("EventsUsers"); //参与人数据
		$eventuh["uzuid"]=$this->shop_uid;
		$eventuh["eventid"]=$event["id"];
		$user = current($EVU->where($eventuh)->getField("id,score,addtime"));
		if(!$user){
			$eventuh["score"]=$event["tms_newcomer"];
			$eventuh["addtime"]=date("Y-m-d H:i:s");
			$eventuh["sid"] = $this->sid;
			$eventuh["id"] = $EVU->add($eventuh);
			$user = $eventuh;
		}
		
		$seller_nick = M("ShopCfg")->where(Array("sid"=>$this->sid))->getField("usernick");
		$this->index_json["seller_nick"] = $seller_nick;
		$this->index_json["getway"]=C("KXDR_GETWAY");
		$this->index_json["user"]=$user;
		$this->index_json["event"]=$event;
		$this->index_json["gifts"]=$gifts;
		$this->index_json["mygifts"]=$mygifts;
		$this->index_json["display"]=$event["curtool"];
		
		//使用原生的Smarty：
		// Vendor("Smarty.Smarty#class");
		// $Smarty = new \Smarty();
		// $Smarty->assign("user",$user);
		// $Smarty->assign("event",$event);
		// $Smarty->assign("getway",C("KXDR_GETWAY"));
		// $Smarty->assign("user",$user);
		// $Smarty->assign("event",$event);
		// $Smarty->assign("gifts",$gifts);
		// $Smarty->assign("mygifts",$mygifts);
		// $TPL = M("EventsTemplates");
		// $tpl = $TPL->where(Array("eid"=>1))->getField("tpl");
		// $Smarty->display('string:'.$tpl);
	}
	public function lott(){
		$eid = I("eid");
		$daily = I("daily");
		$fields = "id,start_time,end_time,brandid,likeme,shareme,followme,collect,tms_everyday,tms_newcomer,lotts";
		if(!$eid){
			$event = current(M("ShopCfg")->where(Array("sid"=>$this->sid))->getField($fields));
			$event["id"]=0;
		}else{
			$EV = M("Events");
			$wh["sid"]=$this->sid;
			$wh["id"]=$eid;
			$event = current($EV->where($wh)->getField($fields));
		}

		$LOTT = D("Lottery");
		$LOTT->sid = $this->sid;
		$LOTT->eid = $event["id"];
		$LOTT->shopcfg = $event; 
		$reda = $LOTT->check_event();
		if($reda["status"]==false){
			$this->ajaxReturn($reda);
			die();
		}
		
		$LOTT->shop_uid = $this->shop_uid;
		$ure = $LOTT->check_user();
		if($ure["status"]=="mission"){
			$this->ajaxReturn($reda);
			die();
		}
		
		$GFT = M("EventsGifts");
		if($daily){
			$gwh["sid"]=$this->sid;
			$gwh["event_id"]=0;
		}else{
			$gwh["sid"]=$this->sid;
			$gwh["event_id"]=$eid;
		}
		$gifts = $GFT->where($gwh)->order("`level` ASC")->getField("`id`,`gift_id`,`type`,`probability`,`per_user`,`quantity`,`advance`,`advance_wins`,`advance_lotts_multiple`,`ext_rule`, `every_minute`, `range_time_start`, `range_time_end`, `every_lotts`, `advance_quantity`,`advance_probability`,`num_iids`,`level_name` AS `level`,`name`,`pic_url`");

		$LOTT->sys_uid=$this->sys_uid;
		$LOTT->gifts=$gifts;
		$LOTT->check_gift(); //检测高级概率
		$lore = $LOTT->do_lott();
		
		$this->ajaxReturn($lore);
	}
}